import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:package_info_plus/package_info_plus.dart';

/// Android原生登录请求模型
/// 对应Android的登录接口参数
class LoginReqNative {
  /// App版本名称，如 "1.0.0"
  final String appVersion;
  
  /// App版本号，如 "100"
  final String appVersionCode;
  
  /// 手机号
  final String mobile;
  
  /// 密码
  final String password;
  
  /// 手机厂商，如 "Xiaomi"、"Apple"
  final String phoneFirm;
  
  /// 手机型号，如 "Mi 11"、"iPhone 14"
  final String phoneModel;
  
  /// 设备唯一标识
  final String phoneUuid;
  
  /// 手机号区域，如 "86"(中国)
  final String smsRegion;
  
  LoginReqNative({
    required this.appVersion,
    required this.appVersionCode,
    required this.mobile,
    required this.password,
    required this.phoneFirm,
    required this.phoneModel,
    required this.phoneUuid,
    this.smsRegion = '86',
  });
  
  /// 从手机号和密码创建登录请求（自动获取设备信息）
  static Future<LoginReqNative> create({
    required String mobile,
    required String password,
    String smsRegion = '86',
  }) async {
    try {
      // 获取应用版本信息
      final packageInfo = await PackageInfo.fromPlatform();
      final appVersion = packageInfo.version;
      final appVersionCode = packageInfo.buildNumber;
      
      // 获取设备信息
      final deviceInfo = DeviceInfoPlugin();
      String phoneFirm = 'Unknown';
      String phoneModel = 'Unknown';
      String phoneUuid = 'Unknown';
      
      if (Platform.isAndroid) {
        final androidInfo = await deviceInfo.androidInfo;
        phoneFirm = androidInfo.manufacturer ?? 'Unknown';
        phoneModel = androidInfo.model ?? 'Unknown';
        phoneUuid = androidInfo.id ?? 'Unknown';
      } else if (Platform.isIOS) {
        final iosInfo = await deviceInfo.iosInfo;
        phoneFirm = 'Apple';
        phoneModel = iosInfo.utsname.machine ?? 'Unknown';
        phoneUuid = iosInfo.identifierForVendor ?? 'Unknown';
      }
      
      debugPrint('📱 设备信息收集完成：');
      debugPrint('  - 厂商: $phoneFirm');
      debugPrint('  - 型号: $phoneModel');
      debugPrint('  - UUID: $phoneUuid');
      debugPrint('  - 版本: $appVersion ($appVersionCode)');
      
      return LoginReqNative(
        appVersion: appVersion,
        appVersionCode: appVersionCode,
        mobile: mobile,
        password: password,
        phoneFirm: phoneFirm,
        phoneModel: phoneModel,
        phoneUuid: phoneUuid,
        smsRegion: smsRegion,
      );
    } catch (e) {
      debugPrint('❌ 获取设备信息失败: $e');
      
      // 失败时使用默认值
      return LoginReqNative(
        appVersion: '1.0.0',
        appVersionCode: '1',
        mobile: mobile,
        password: password,
        phoneFirm: 'Unknown',
        phoneModel: 'Unknown',
        phoneUuid: 'Unknown',
        smsRegion: smsRegion,
      );
    }
  }
  
  Map<String, dynamic> toJson() {
    return {
      'appVersion': appVersion,
      'appVersionCode': appVersionCode,
      'mobile': mobile,
      'password': password,
      'phoneFirm': phoneFirm,
      'phoneModel': phoneModel,
      'phoneUuid': phoneUuid,
      'smsRegion': smsRegion,
    };
  }
  
  @override
  String toString() {
    return 'LoginReqNative(mobile: $mobile, phoneFirm: $phoneFirm, phoneModel: $phoneModel)';
  }
}

